# R code for the paper titled
# The Watching Eyes Effect Predicts In-Game Infractions for Professional Sports
library(lme4)
library(lmerTest)
library(lmeresampler)
library(emmeans)
library(MuMIn)

nfl_eyes_data <- read.csv('nfl_eyes_data_OSF_3.csv')
# hist(nfl_eyes_data$PENALTIES, xlab = "Penalties",
#      col="lightblue1",
#      border="dodgerblue3", main = "Penalties Histogram")

eyes_model <- lmer(PENALTIES ~ logo_eyes + 
                 CLOSING.SPREAD +
                 FINAL +
                 WEEK +
                 VENUE +
                 year +
                 (1|GAME.ID) +
                 (1|TEAM), data = nfl_eyes_data)

summary(eyes_model)
emmeans(eyes_model, pairwise ~ logo_eyes, lmer.df  = "satterthwaite", adjust = "bonf", lmerTest.limit = 10000)
r.squaredGLMM(eyes_model) 

lmer_boot <- bootstrap(eyes_model, .f = fixef, type = "parametric", B = 5000)
confint(lmer_boot)
print(lmer_boot,  ci = TRUE)

cor.test(nfl_eyes_data$pixels_average, nfl_eyes_data$PENALTIES, method = 'pearson')

pixel_model <- lmer(PENALTIES ~ pixels_average +
                     CLOSING.SPREAD +
                     FINAL +
                     WEEK +
                     VENUE +
                     year +
                     (1|GAME.ID) +
                     (1|TEAM), data = nfl_eyes_data)

summary(pixel_model)
r.squaredGLMM(pixel_model)
